package mercurius.fitness.client.rb.diet;

import mercurius.fitness.client.Fitness;
import mercurius.fitness.client.rb.diet.forms.AddDietForm;
import mercurius.fitness.client.rb.diet.forms.EditDietForm;
import mercurius.fitness.client.rb.diet.forms.ViewDietForm;
import net.pleso.framework.client.bl.IAuthDataSource;
import net.pleso.framework.client.bl.actions.IDeleteRowExecutor;
import net.pleso.framework.client.bl.forms.IAddForm;
import net.pleso.framework.client.bl.forms.IEditForm;
import net.pleso.framework.client.bl.forms.IViewForm;
import net.pleso.framework.client.bl.providers.IActionProvider;
import net.pleso.framework.client.bl.providers.IActionProviders;
import net.pleso.framework.client.bl.providers.IAddFormProvider;
import net.pleso.framework.client.bl.providers.IDeleteRowProvider;
import net.pleso.framework.client.bl.providers.IEditFormProvider;
import net.pleso.framework.client.bl.providers.IViewFormProvider;
import net.pleso.framework.client.bl.rb.IRB;
import net.pleso.framework.client.bl.rb.columns.IRBColumn;
import net.pleso.framework.client.bl.rb.columns.impl.RBColumn;
import net.pleso.framework.client.dal.IDataRow;
import net.pleso.framework.client.dal.SelectParams;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class DietRB implements IRB, IActionProviders {
	
	private String type = "";
	
	public DietRB(String type) {
		this.type = type;
	}
	
	private class AddRowAction implements IAddFormProvider {

		public IAddForm getAddForm(IDataRow row) {
			return new AddDietForm(type);
		}

		public String getActionCaption() {
			return null;
		}
		
	}
	
	private class ViewRowAction implements IViewFormProvider {

		public IViewForm getViewForm(IDataRow row) {
			
			if (row instanceof Diet && row != null) {
				GWT.log(((Diet) row).getType(),null);
				return new ViewDietForm(((Diet)row).getID(),type);
			}
			else
				return new ViewDietForm(null,type);
		}

		public String getActionCaption() {
			return null;
		}
		
	}
	
	private class EditRowAction implements IEditFormProvider {

		public IEditForm getEditForm(IDataRow row) {
			if (row instanceof Diet && row != null)
				return new EditDietForm(((Diet)row).getID(),type);
			else
				return new EditDietForm(null,type);
		}

		public String getActionCaption() {
			return null;
		}
		
	}

	private class DeleteRowAction implements IDeleteRowProvider, IDeleteRowExecutor {

		public IDeleteRowExecutor getDeleteRowExecutor() {
			return this;
		}

		public String getActionCaption() {
			return null;
		}

		public void deleteRow(IDataRow row, AsyncCallback callback) {
			dataSet.delete(((Diet) row).getID(), callback,DietRB.this.type); 			
		}
		
	}

	private static DietDataSet dataSet = new DietDataSet();
	
	public static DietDataSet getDataSet() {
		return dataSet;
	}
	
	private class RBDataSource implements IAuthDataSource {
		
		public void select(SelectParams params, AsyncCallback callback) {
			params.setLimit(Fitness.get().getDefaultRowLimit());
			dataSet.select(params, callback,DietRB.this.type);
		}

		public void selectCount(SelectParams params, AsyncCallback callback) {
			params.setLimit(Fitness.get().getDefaultRowLimit());
			dataSet.selectCount(callback,DietRB.this.type);
		}
	}

	public String getCaption() {
		String cap = "";
		
		if(type.equals(Diet.TYPE_LOG)) {
			cap = "Diet Log";
		} else if(type.equals(Diet.TYPE_MASTER_REPOSITORY)) {
			cap = "Master Nutrition Repository";
		} else if(type.equals(Diet.TYPE_PRIVATE_REPOSITORY)) {
			cap = "Nutrition Repository";
		}
		return cap;
	}


	public IRBColumn[] getColumns() {
		
		IRBColumn[] rbColumns = null;
		
		
		if(type.equals(Diet.TYPE_LOG)) {
			rbColumns = new RBColumn[] {
					new RBColumn(Diet.Columns.date, 15),
					new RBColumn(Diet.Columns.name, 30),
					new RBColumn(Diet.Columns.calories, 15),
			};
		} else if(type.equals(Diet.TYPE_MASTER_REPOSITORY)) {
			rbColumns = new RBColumn[] {
					new RBColumn(Diet.Columns.name, 30),
					new RBColumn(Diet.Columns.Vitamin_D_calciferol, 30)
			};
		} else if(type.equals(Diet.TYPE_PRIVATE_REPOSITORY)) {
			rbColumns = new RBColumn[] {
					new RBColumn(Diet.Columns.name, 30),
					new RBColumn(Diet.Columns.amount, 5),
					new RBColumn(Diet.Columns.amount_measurement, 5),
					new RBColumn(Diet.Columns.calories, 10),
					new RBColumn(Diet.Columns.fat_total, 5),
					new RBColumn(Diet.Columns.carb_total, 5),
					new RBColumn(Diet.Columns.protein, 5)
			};
		}
		return rbColumns;
	}

	public IAuthDataSource getDataSource() {
		return new RBDataSource();
	}

	public IActionProvider[] getActionProviders() {
		return new IActionProvider[] {
			//	new AddRowAction(),
				new ViewRowAction(),
				new DeleteRowAction(),
				new EditRowAction()
		};
	}

}
